
import {
  getServServiceConfListApi,
  getServServiceConfDetailApi,
  updateServServiceConfApi,
  modifySlopeApi
} from '@/services/serviceManagementSettings.js';
import { message } from 'ant-design-vue';
export default {
  namespaced: true,
  state: {
    servServiceConfLoading: false,
    servServiceConfList: [],
    servServiceConfDetail: {},
    updateServServiceConfLoading: false,
    slopeLoading: false,
    paginationOptions: {
      pageNum: 1,
      pageSize: 10,
      total: 10
    }
  },
  mutations: {
    setState(state, { key, payload }) {
      state[key] = payload;
    }
  },
  actions: {
    // 获取事件监控
    async getServServiceConfList({ commit, state }, params) {
      commit('setState', { key: 'servServiceConfLoading', payload: true });
      const { pageNum, pageSize } = state.paginationOptions;
      const apiParams = {
        pageNum,
        pageSize,
        ...params
      };
      const rep = await getServServiceConfListApi(apiParams);
      commit('setState', { key: 'paginationOptions', payload: { ...state.paginationOptions, total: rep.data.total }});
      commit('setState', { key: 'servServiceConfList', payload: rep.data.list });
      commit('setState', { key: 'servServiceConfLoading', payload: false });
    },
    async getServServiceConfDetail({ commit, state }, params) {
      commit('setState', { key: 'servServiceConfLoading', payload: true });
      const rep = await getServServiceConfDetailApi(params);
      rep.data.repReportTemplates[0].item = rep.data.item;
      commit('setState', { key: 'servServiceConfDetail', payload: rep.data });
      commit('setState', { key: 'servServiceConfLoading', payload: false });
    },
    // 修改
    async updateServServiceConf({ commit }, params) {
      commit('setState', { key: 'updateServServiceConfLoading', payload: true });
      try {
        const rep = await updateServServiceConfApi(params);
        if (rep.code === 200) {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'updateServServiceConfLoading', payload: false });
      }
    },
    async modifySlope({ commit }, params) {
      commit('setState', { key: 'slopeLoading', payload: true });
      try {
        const rep = await modifySlopeApi(params);
        if (rep.code === 200) {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'slopeLoading', payload: false });
      }
    }
  }
};
